Imports System.Data.SqlClient

Friend Class DBLogin_Factory
    Inherits DBBase

    Public Function GetAllLoginsForDatabase( _
                            ByVal lDatabaseID As Long) As Collection

        Dim oList As New Collection

        Dim sSQL = "select distinct"
        sSQL = sSQL & "     a.[ID]"
        sSQL = sSQL & " from"
        sSQL = sSQL & "     tDDLogins a"
        sSQL = sSQL & " where"
        sSQL = sSQL & "     a.[DatabaseID]=" & ValueToDBValue(lDatabaseID)
        sSQL = sSQL & "     and a.[IsDeleted]=0"

        Dim oData As DataTable = MyBase.ExecuteReadToDataTable(sSQL)

        For Each oRow As DataRow In oData.Rows
            Dim oLogin As New SQLAutoDocLib.Login(oRow.Item(0))
            oLogin.Load()

            oList.Add(oLogin, oLogin.ID)
        Next

        oData.Dispose()

        Return oList
    End Function

    Public Function GetAllLoginsForDatabase( _
                    ByVal lDatabaseID As Long, _
                    ByVal sStartDate As String, _
                    ByVal sEndDate As String) As Collection

        Dim oList As New Collection

        Dim sSQL = "select distinct"
        sSQL = sSQL & "     a.[ID]"
        sSQL = sSQL & " from"
        sSQL = sSQL & "     tDDLogins a"
        sSQL = sSQL & " where"
        sSQL = sSQL & "     a.[DatabaseID]=" & ValueToDBValue(lDatabaseID)
        sSQL = sSQL & "     and a.[IsDeleted]=0"
        sSQL = sSQL & "     and isnull(a.[ChangeDate],a.[CreateDate]) between " & ValueToDBValue(sStartDate) & " and " & ValueToDBValue(sEndDate)

        Dim oData As DataTable = MyBase.ExecuteReadToDataTable(sSQL)

        For Each oRow As DataRow In oData.Rows
            Dim oLogin As New SQLAutoDocLib.Login(oRow.Item(0))
            oLogin.Load()

            oList.Add(oLogin, oLogin.ID)
        Next

        oData.Dispose()

        Return oList
    End Function

    Public Function GetLoginByName( _
                        ByVal lDatabaseID As Long, _
                        ByVal sLogin As String) As SQLAutoDocLib.Login

        Dim oLogin As SQLAutoDocLib.Login = Nothing

        Dim sSQL As String = "select distinct"
        sSQL = sSQL & "     [ID]"
        sSQL = sSQL & " from"
        sSQL = sSQL & "     tDDLogins"
        sSQL = sSQL & " where"
        sSQL = sSQL & "     [DatabaseID]=" & ValueToDBValue(lDatabaseID)
        sSQL = sSQL & "     and [Login]=" & ValueToDBValue(sLogin)

        Dim oValue As Object = MyBase.ExecuteScalar(sSQL)

        If IsNumeric(oValue) Then
            oLogin = New SQLAutoDocLib.Login(CLng(oValue))
            oLogin.Load()
        End If

        Return oLogin
    End Function

    Public Function CountOfActiveLoginsForDatabase(ByVal lDatabaseID As Long) As Integer
        Dim iCount As Integer = 0

        Dim sSQL As String = "select "
        sSQL = sSQL & "     count(distinct [ID])"
        sSQL = sSQL & " from"
        sSQL = sSQL & "     tDDLogins"
        sSQL = sSQL & " where"
        sSQL = sSQL & "     [DatabaseID]=" & ValueToDBValue(lDatabaseID)
        sSQL = sSQL & "     and [IsDeleted]=0"

        Dim oValue As Object = MyBase.ExecuteScalar(sSQL)

        If IsNumeric(oValue) Then
            iCount = Val(oValue)
        End If

        Return iCount
    End Function
End Class
